package org.example.callgenerateclassobject;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *  通过反射的方式调用
 */

public class Method1 {

    public static void main(String[] args) throws NotFoundException, CannotCompileException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        ClassPool pool = ClassPool.getDefault();
        pool.appendClassPath("F:\\javacode\\javassistDemo\\javassistDemo\\src\\main\\java");
        CtClass cc = pool.get("org.example.Person");
        // 获取CtClass直接实例化
        Object person = cc.toClass().newInstance();
        // 设置值
        Method setName = person.getClass().getMethod("setName", String.class);
        setName.invoke(person, "sizengli");
        // 输出值
        Method execute = person.getClass().getMethod("printName", null);
        execute.invoke(person, null);


//        //执行有参构造器，然后再调用打印名字的方法
//        Method argsConstruct = person.getClass().getMethod("Person", null);
//        argsConstruct.invoke(person, "zanzan");
//        //输出值
//        execute.invoke(person, null);
//        CtConstructor declaredConstructor = cc.getDeclaredConstructor(String.class);
//        declaredConstructor.
    }
}
